package com.haiwei.poi.word.poi;

import com.haiwei.poi.word.bean.HaiweiBeanUtil;
import com.haiwei.poi.word.manager.HaiweiPatternUtil;
import com.haiwei.poi.word.sign.HaiweiSignParser;
import com.haiwei.poi.word.sign.HaiweiTextSign;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.xmlbeans.XmlCursor;
import org.apache.xmlbeans.XmlObject;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTR;

import java.util.Formatter;

/**
 * 文本框处理工具类.
 *
 * @date: 2022-06-25
 * @version: 1.0
 * @author: karry@haiwei.com
 */
public class HaiweiTextBoxUtil {
    
    /**
     * 处理文本框
     * @param paragraph
     * @param data
     * @param isDetail
     */
    public static void parseByXPath(XWPFParagraph paragraph) {
        String nameSpace = "declare namespace o='urn:schemas-microsoft-com:office:office'; " +
                "declare namespace r='http://schemas.openxmlformats.org/officeDocument/2006/relationships'; " +
                "declare namespace mc='http://schemas.openxmlformats.org/markup-compatibility/2006'; " +
                "declare namespace m='http://schemas.openxmlformats.org/officeDocument/2006/math'; " +
                "declare namespace v='urn:schemas-microsoft-com:vml'; " +
                "declare namespace wp='http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing'; " +
                "declare namespace w10='urn:schemas-microsoft-com:office:word'; " +
                "declare namespace w='http://schemas.openxmlformats.org/wordprocessingml/2006/main'; " +
                "declare namespace wps='http://schemas.microsoft.com/office/word/2010/wordprocessingShape';" +
                "declare namespace wne='http://schemas.microsoft.com/office/word/2006/wordml';";
        String xPath = "$this//wps:txbx/w:txbxContent/w:p/w:r/w:t";
        
        CTR[] ctr = paragraph.getCTP().getRArray();
        if(ctr.length == 0) {
            return;
        }
        XmlObject object = ctr[0];
        XmlCursor selectPathCursor = getXmlObjectByXPath(object,nameSpace,xPath);
        int count = selectPathCursor.getSelectionCount();
        if(count ==0 ) {
            return;
        }
        StringBuilder txbxBuilder = new StringBuilder();
        selectPathCursor.push();
        while (selectPathCursor.toNextSelection()) {
            String text = selectPathCursor.getTextValue();
            txbxBuilder.append(text);
            selectPathCursor.setTextValue("");
        }
        selectPathCursor = getXmlObjectByXPath(object,nameSpace,xPath);
        if(selectPathCursor.hasNextSelection()){
            selectPathCursor.toNextSelection();
            //String text = parseMatch(txbxBuilder.toString(), data,null, isDetail,null);
            String text = txbxBuilder.toString();
            if(HaiweiPatternUtil.containPlaceHolder(text)){
                // 解析标签
                HaiweiTextSign sign = HaiweiSignParser.parse(text);
                // 根据路径获取值
                Object value = HaiweiBeanUtil.getValue(sign.getPath());
                // 填充内容,需要拼接起来
                text = sign.getBeforSignContext() + value + sign.getAfterSignContext();
                selectPathCursor.setTextValue(text);
            }
        }
        selectPathCursor.dispose();
    }
    
    /**
     * 获取xml游标
     * @param xml
     * @param nameSpace
     * @param xpath
     * @return
     */
    private static XmlCursor getXmlObjectByXPath(XmlObject xml, String nameSpace,String xpath) {
        XmlCursor xpathCursor = xml.newCursor();
        xpathCursor.toLastChild();
        StringBuilder builder = new StringBuilder();
        Formatter formatter = new Formatter(builder);
        formatter.format("%s %s", nameSpace, xpath);
        xpathCursor.selectPath(formatter.toString());
        formatter.close();
        return xpathCursor;
    }
}
